/**
 * lilang
 * PofBulletinAction.java
 * 2013-10-13下午6:56:34
 */
package com.cq.oa.web.pof.action;

import java.util.List;

import oa.base.session.SessionManger;
import oa.base.util.Constants;
import oa.base.util.DateUtil;
import oa.base.util.Page;
import oa.base.web.Action.BaseAction;

import com.cq.oa.domain.pof.WorkMgmt;
import com.cq.oa.service.pof.WorkMgmtService;


/**
 * @author lilang
 *
 */
@SuppressWarnings("serial")
public class PofWorkMgmtAction extends BaseAction<WorkMgmt>{

	/* (non-Javadoc)
	 * @see oa.base.web.Action.BaseAction#getEntityClass()
	 */
	@Override
	protected Class<WorkMgmt> getEntityClass() {
		return WorkMgmt.class;
	}
	
	private String admin;
	
	private WorkMgmtService workMgmtService;
	
	private List<WorkMgmt> mgmts;
	
	
	
	
	public List<WorkMgmt> getMgmts() {
		return mgmts;
	}

	public void setMgmts(List<WorkMgmt> mgmts) {
		this.mgmts = mgmts;
	}

	public String getAdmin() {
		return admin;
	}

	public void setAdmin(String admin) {
		this.admin = admin;
	}

	public void setWorkMgmtService(WorkMgmtService workMgmtService) {
		this.workMgmtService = workMgmtService;
	}

	public String operate(){
		if(null!=entity.getSid()){
			entity = this.workMgmtService.findByParmkeys(entity.getSid());
		}
		return "operate";
	}
	
	public String save(){
		initEntry(entity);
		if(this.workMgmtService.isUnique(entity, "orgSid,userId,sendYear,sendMonth")){
			if(null!=entity.getSid()){
				this.workMgmtService.update(entity);
			}else{
				this.workMgmtService.create(entity);
			}
			this.renderJsonSuccess();
		}else{
			this.renderJsonError("已经录入了"+entity.getSendYear()+"年"+entity.getSendMonth()+"月"+entity.getCname()+"的工资信息");
		}
		return JSON;
	}
	
	private void initEntry(WorkMgmt mgmt){
		if(mgmt!=null){
			if(mgmt.getSendTime()!=null){
				mgmt.setSendYear(DateUtil.getYear(mgmt.getSendTime()));
				mgmt.setSendMonth(DateUtil.getMonth(mgmt.getSendTime()));
			}
		}
		entity.setOrgSid(SessionManger.getOrgId());
	}
	
	
	public String findPage() throws Exception{
		filterMap.put(Constants.WHERE_LIKE+"cname", entity.getCname());
		filterMap.put("userId", SessionManger.getUserId());
		filterMap.put(Constants.ORG_SID, SessionManger.getOrgId());
		filterMap.put("sendYear", entity.getSendYear());
		filterMap.put("sendMonth", entity.getSendMonth());
		Page<WorkMgmt> page = this.workMgmtService.findByPage(filterMap,linkMap,shortMap, super.getPageNo(), super.getPageSize());
		this.renderJsonSuccess(jsonUtils.getExtJson(page, new String[] {}));
		return JSON;
	}
	
	
	
	
	
	public String delete(){
		this.workMgmtService.delete(entity.getSid());
		this.renderJsonSuccess();
		return JSON;
	}
	
	public String detail(){
		
		return "detail";
	}
	
	public String searchWork() throws Exception{
		filterMap.put(Constants.WHERE_LIKE+"cname", entity.getCname());
		filterMap.put("userId", SessionManger.getUserId());
		filterMap.put(Constants.ORG_SID, SessionManger.getOrgId());
		filterMap.put("sendYear", entity.getSendYear());
		filterMap.put("sendMonth", entity.getSendMonth());
		entity = this.workMgmtService.findUnObject(filterMap);
		this.renderJsonSuccess(jsonUtils.getExtJson(entity,new String[] {}));
		return JSON;
	}

}
